<?php

namespace app\admin\controller;

use app\service\FinancialLogService;
use app\service\MemberService;

class FinancialLog extends Admin
{
    public function __construct(FinancialLogService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    public function index()
    {
        if (!$this->request->isPost()) {
            $this->assign("title", "流水日志");
            $this->assign("member_id", $this->request->get('member_id'));
            return $this->fetch("index");
        }
        $params = $this->request->post();
        $where = [];
        if (!empty($params["create_at"])) {
            $between = explode(" - ", $params["create_at"]);
            if (count($between) == 2) {
                $where[] = ["create_at", "between", [$between[0], $between[1]]];
            }
        }
        if (!empty($params["flow_type"])) {
            $where[] = ['flow_type', '=', $params["flow_type"]];
        }
        if (!empty($params["order_sn"])) {
            $where[] = ['order_sn', '=', trim($params["order_sn"])];
        }
        $email = trim($params["email"]);
        $memberId = trim($params["member_id"]);
        $memberIds = [];
        if ($email) {
            $memberService = $this->app->make(MemberService::class);
            $memberIds = $memberService->Column([["email", "like", "%$email%"]], "id");
            if (!$memberIds) {
                $ret = ["rows" => [
                    "total" => 0,
                    "list" => [],
                ]];
                $this->result($ret);
            }
        }
        if (!empty($memberId)) {
            array_push($memberIds, $memberId);
        }
        if (!empty($memberIds)) {
            $where[] = ['member_id', 'in', $memberIds];
        }
        if (!empty($status)) {
            $where[] = ['status', '=', $status];
        }
        $ret = $this->service->getList($where, ["*"], ["member", "coin"]);
        int_to_string($ret["rows"]["list"], ["flow_type" => config("global.TRADE_LIST_MAP")]);
        $this->result($ret);
    }
}